﻿using Newtonsoft.Json;
using QG.FrontDesk.BLL.IService;
using QG.FrontDesk.com.Attbutes;
using QG.FrontDesk.com.Models;
using QG.FrontDesk.Core.Encrypt;
using QG.FrontDesk.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace QG.FrontDesk.com.Controllers
{
    [PermissionAttribute]
    public class AccountController : Controller
    {
        private IUserInfoService _userInfoService;

        public AccountController(IUserInfoService userInfoService)
        {
            DataContext<Company>.context.model = null;
            DataContext<UserInfo>.context.model = null;
            //获取存在的cookie
            //再次浏览器判断是否有值
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
            if (cookie != null)
            {
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                string userData = ticket.UserData;
                if (ticket.Name == "求职")
                {
                    DataContext<UserInfo>.context.model = JsonConvert.DeserializeObject<UserInfo>(userData);
                }
                else if (ticket.Name == "公司")
                {
                    DataContext<Company>.context.model = JsonConvert.DeserializeObject<Company>(userData);
                }
            }
            _userInfoService = userInfoService;
        }

        ResponseResult result = new ResponseResult();

        /// <summary>
        /// 账号绑定页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AccountInfo()
        {

            int userId = DataContext<UserInfo>.context.model.UserId;
            ViewBag.userInfo = _userInfoService.GetUserInfo(userId);
            return View();
        }
        /// <summary>
        /// 修改密码页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdatePwd()
        {
            int userId = DataContext<UserInfo>.context.model.UserId;

            ViewBag.userInfo = _userInfoService.GetUserInfo(userId);
            return View();
        }
        /// <summary>
        /// 修改密码操作
        /// </summary>
        /// <returns></returns>
        public JsonResult UpdatePassword(string oldpassword,string newpassword) {
            oldpassword = MD5Encrypt.Encrypt(oldpassword);
            newpassword = MD5Encrypt.Encrypt(newpassword);
            int usrId = DataContext<UserInfo>.context.model.UserId;
            result.success = _userInfoService.UpdatePassword(oldpassword,newpassword, usrId) >0;
            return Json(result);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [Permission]
        public JsonResult UpdateUserInfo(UserInfo user) {
            user.UserId = DataContext<UserInfo>.context.model.UserId;
            result.success = _userInfoService.UpdateUserInfo(user)>0;
            if (result.success)
            {
                DataContext<UserInfo>.context.model.UserName = user.UserName;
            }
            result.data = user;
            return Json(result);
        }
        
    }
}